//
//  WDInterstitialManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/1.
//

#import "WDInterstitialManager.h"
#import "WDAdStatusManager.h"
#import "WDCallBackManager.h"
#import "WDAdStatusManager.h"
#import <AppLovinSDK/AppLovinSDK.h>
#import "WDJsonManager.h"
@import Adjust;

@interface WDInterstitialManager ()<MAAdDelegate>
@property (nonatomic, assign) BOOL hasShowAd;
@property (nonatomic, strong) MAInterstitialAd *interstitialAd;
@property (nonatomic, assign) NSInteger retryAttempt;
@end
@implementation WDInterstitialManager
static WDInterstitialManager *_interstitial = nil;
+ (instancetype)shareInterstitialManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _interstitial = [[self alloc] init];
        
    });
    return _interstitial;
}

- (void)interstitialDelegate
{
    if ( [WDJsonManager maxInterForSDKConfigFile].length>4) {
        self.interstitialAd = [[MAInterstitialAd alloc] initWithAdUnitIdentifier: [WDJsonManager maxInterForSDKConfigFile]];
        self.interstitialAd.delegate = self;
        [self.interstitialAd loadAd];}
    }


- (void)showInterstitial
{
    [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"WDOrientation"];

        if ( [self.interstitialAd isReady] )
    {
    [self.interstitialAd showAd];

    }else{
        self.hasShowAd = YES;
        [self loadInter];
    }

}

- (void)loadInter
{
    [[WDAdStatusManager shareStatusManager] adLoadWithEventName:INTER_EVENT_NAME];
    
    if (![self.interstitialAd isReady] )
    {
        [self.interstitialAd loadAd];

    }
}




#pragma mark - MAAdDelegate Protocol

/**
 * 插页广告加载成功回调
 *
 * @param ad 加载成功的广告对象
 */
- (void)didLoadAd:(MAAd *)ad
{
    // 插页广告已准备好展示。'[self.interstitialAd isReady]' 现在将返回 'YES'
    // 重置重试次数
    self.retryAttempt = 0;
        [[WDAdStatusManager shareStatusManager] adLoadFillSuccessWithEventName:INTER_EVENT_NAME];
    if (self.hasShowAd)
    {
        self.hasShowAd = NO;
        [self showInterstitial];
    }
}

/**
 * 插页广告加载失败回调
 *
 * @param adUnitIdentifier 广告单元标识符
 * @param error 加载失败错误信息
 */
- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error
{
    // 插页广告加载失败。AppLovin 推荐您使用指数级递增的延迟重试，直到最大延迟（在此情况下为64秒）
    self.retryAttempt++;
    NSInteger delaySec = pow(2, MIN(6, self.retryAttempt));
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delaySec * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self loadInter];
    });
}


/**
 * 插页广告展示回调
 *
 * @param ad 展示的广告对象
 */
- (void)didDisplayAd:(MAAd *)ad
{
    NSLog(@"--------%s--------%@",__PRETTY_FUNCTION__,ad.description);
    [[WDAdStatusManager shareStatusManager] adShowWithEventName:INTER_EVENT_NAME];
    [[WDCallBackManager shareCallBackManager] adCallBackWithType:Show adSpace:inter message:@""];
    NSNumber *revenue =[NSNumber numberWithDouble:ad.revenue*1000];
    [[WDAdStatusManager shareStatusManager] adRevenue:@"inter" source:ad.networkName revenue:revenue currency:@"USD"];
    ADJAdRevenue *adjustAdRevenue = [[ADJAdRevenue alloc] initWithSource: ADJAdRevenueSourceAppLovinMAX];
    [adjustAdRevenue setRevenue: ad.revenue currency: @"USD"];
    [adjustAdRevenue setAdRevenueNetwork: ad.networkName];
    [adjustAdRevenue setAdRevenueUnit: ad.adUnitIdentifier];
    [adjustAdRevenue setAdRevenuePlacement: ad.placement];
    [Adjust trackAdRevenue: adjustAdRevenue];

}

/**
 * 插页广告点击回调
 *
 * @param ad 被点击的广告对象
 */
- (void)didClickAd:(MAAd *)ad
{
}

/**
 * 插页广告隐藏回调
 *
 * @param ad 隐藏的广告对象
 */
- (void)didHideAd:(MAAd *)ad
{
    self.hasShowAd = NO;
    [[WDCallBackManager shareCallBackManager] adCallBackWithType:Finish adSpace:inter message:@""];
    [[WDAdStatusManager shareStatusManager] adCloseWithEventName:INTER_EVENT_NAME];
    // 插页广告已隐藏。预加载下一个广告
    [self loadInter];
}

/**
 * 插页广告展示失败回调
 *
 * @param ad 展示失败的广告对象
 * @param error 展示失败错误信息
 */
- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error
{
    // 插页广告展示失败。AppLovin 推荐您加载下一个广告
    [self loadInter];
}

@end
